﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;

public class LoadAssetsFromFile : MonoBehaviour
{
    string sharABdir = "AssetBundles/share.unity3d";
    string cubeABdir = "AssetBundles/scene/cube.unity3d";
    void Start()
    {
       // AssetBundle shareAB = AssetBundle.LoadFromFile("AssetBundles/share.unity3d");//首先加载cube所依赖的AssetBundel
        LoadManifest();
        //StartCoroutine("LoadFromMemoryAsync");
        //LoadFromMemory();
        //LoadFormFile();
        //StartCoroutine("LoadFormFileAsync");
        //StartCoroutine(LoadFromWebServer());
    }
    //从内存里面加载AB,异步方式
    IEnumerator LoadFromMemoryAsync()
    {
        AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(cubeABdir));
        yield return request;
        AssetBundle ab = request.assetBundle;
        GameObject cubePrefab = ab.LoadAsset<GameObject>("Cube");
        if (cubePrefab != null)
        {
            Debug.Log("从内存里面异步加载成功");
            Instantiate(cubePrefab);
        }
    }
    //从内存里面加载AB,同步方式
    void LoadFromMemory()
    {
        AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(cubeABdir));
        GameObject cubePrefab = ab.LoadAsset<GameObject>("Cube");
        if (cubePrefab != null)
        {
            Debug.Log("从内存里面同步加载成功");
            Instantiate(cubePrefab);
        }
    }
    //从文件加载ab,
    void LoadFormFile()
    {
        AssetBundle ab = AssetBundle.LoadFromFile(cubeABdir);
        GameObject cubePrefab = ab.LoadAsset<GameObject>("Cube");
        if (cubePrefab != null)
        {
            Debug.Log("从本地文件同步加载加载成功");
            Instantiate(cubePrefab);
        }
    }
    //从本地异步加载
    IEnumerator LoadFormFileAsync()
    {
        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(cubeABdir);
        yield return request;
        AssetBundle ab = request.assetBundle;
        GameObject cubePrefab = ab.LoadAsset<GameObject>("Cube");
        if (cubePrefab != null)
        {
            Debug.Log("从本地文件异步加载加载成功");
            Instantiate(cubePrefab);
        }
    }

    //从服务器加载AB包
    IEnumerator LoadFromWebServer()
    {
        string uri = @"http://127.0.0.1/" + cubeABdir;
        UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
        yield return request.Send();
        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);//方法一
                                                                        // AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;//方法二
        GameObject cubePrefab = ab.LoadAsset<GameObject>("Cube");
        if (cubePrefab != null)
        {
            Debug.Log("从服务器加载成功");
            Instantiate(cubePrefab);
        }
    }
    /// <summary>
    /// 利用.manifest文件加载AB包的依赖
    /// </summary>
    void LoadManifest()
    {
        AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");
        AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        //打印出所有的包名
        foreach (string item in manifest.GetAllAssetBundles())
        {
            Debug.Log(item);
        }
        //加载所有的依赖包
        string[] ABname = manifest.GetAllDependencies("scene/cube.unity3d");
        foreach (string item in ABname)
        {
            AssetBundle dependenciesAb = AssetBundle.LoadFromFile("AssetBundles/"+item);
            if (dependenciesAb!=null)
            {
                Debug.LogFormat("加载依赖{0}成功",dependenciesAb.name);
            }
            else
            {
                Debug.LogFormat("加载依赖{0}失败", item);
            }
        }
    }
}
